<template>
  <div class="login-container">
    <el-form
      class="login-form"
      :rules="rules"
      ref="form"
      :model="user"
      size="medium"
      @submit.prevent="handleSubmit"
    >
      <div class="login-form__header">
        <img
          class="login-logo"
          src=""
          alt="拉勾心选"
        >
      </div>
      <el-form-item prop="email">
        <el-input
          v-model="user.email"
          placeholder="请输入用户名"
        >
          <template #prefix>
            <i class="el-input__icon el-icon-user" />
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="user.password"
          type="password"
          placeholder="请输入密码"
        >
          <template #prefix>
            <i class="el-input__icon el-icon-lock" />
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="code">
        <div class="code-wrap">
          <el-input
            v-model="user.code"
            placeholder="请输入验证码"
          >
            <template #prefix>
              <i class="el-input__icon el-icon-key" />
            </template>
          </el-input>
          <img
            class="code"
            alt="验证码"
            :src="captchaSrc"
          >
        </div>
      </el-form-item>
      <el-form-item>
        <el-button
          class="submit-button"
          type="primary"
          :loading="loading"
          native-type="submit"
        >
          登录
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'
import { getCaptcha, login } from '@/api/common'
import { useRouter, useRoute } from 'vue-router'
import { IElForm } from '@/types/element-plus'
import { useAuthStore } from '@/store'
const authStore = useAuthStore()

const router = useRouter()
const route = useRoute()
const form = ref<IElForm | null>(null)
const captchaSrc = ref('')
const user = reactive({
  email: 'admin',
  password: '123456',
  code: ''
})
const loading = ref(false)
const rules = ref({
  email: [
    { required: true, message: '请输入账号', trigger: 'change' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'change' }
  ],
  code: [
    { required: true, message: '请输入验证码', trigger: 'change' }
  ]
})

onMounted(() => {
  loadCaptcha()
  console.log(form.value)
})

// 获取验证码图片
const loadCaptcha = async () => {
  const data = await getCaptcha()
  captchaSrc.value = URL.createObjectURL(data)
}

// 登录
const handleSubmit = () => {
  // 表单验证
  const valid = form.value?.validate()
  if (!valid) {
    return false
  }

  // 验证通过，展示loading
  loading.value = true

  // // 请求提交
  // login(user).then(res => {
  //   console.log(res)
  //   // 处理响应
  //   // 将 token 存储起来
  //   const token = 'Bearer d259bb85-d46c-47c8-8440-3228b46d832c'
  //   authStore.setToken(token)
  //   router.replace({ name: 'project_list' })
  // }).catch(err => {
  //   console.log(err)
  // }).finally(() => {
  //   loading.value = false
  // })
  const data = login(user).finally(() => {
    loading.value = false
  })
  console.log(data)
  const token = 'Bearer d259bb85-d46c-47c8-8440-3228b46d832c'
  authStore.setToken(token)
  let redirect = route.query.redirect || '/'
  if (typeof redirect !== 'string') {
    redirect = '/'
  }
  router.replace(redirect)
}

</script>

<style lang="scss" scoped>
.login-container {
  min-width: 400px;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #2d3a4b;
}

.login-form {
  padding: 30px;
  border-radius: 6px;
  background: #fff;
  min-width: 350px;
  .login-form__header {
    display: flex;
    justify-content: center;
    align-items: center;
    padding-bottom: 30px;
  }

  .el-form-item:last-child {
    margin-bottom: 0;
  }

  .login__form-title {
    display: flex;
    justify-content: center;
    color: #fff;
  }

  .submit-button {
    width: 100%;
  }

  .login-logo {
    width: 271px;
    height: 74px;
  }
  .code-wrap {
    display: flex;
    align-items: center;
    .code {
      height: 37px;
    }
  }
}
</style>
